﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ExitAnim : MonoBehaviour
{
    public Animator animator; // 引用Animator组件
    public Button transitionButton; // 切换按钮
    public string nextPageName;//NextPageName

    // 定义事件，用于动画完成后的回调
    public event Action OnAnimationCompleteEvent;
    // Start is called before the first frame update
    void Start()
    {
        // 确保按钮和动画控制器已初始化
        if (transitionButton != null)
        {
            transitionButton.onClick.AddListener(StartTransition);
        }

        if (animator == null)
        {
            animator = GetComponent<Animator>();
        }
    }
    private bool isTransitioning = false; // 是否正在播放动画
    private void StartTransition()
    {
        if (!isTransitioning)
        {
            isTransitioning = true;
            StartCoroutine(WaitAnim());
        }
      
       
    }

    public IEnumerator WaitAnim()
    {
        if (animator != null)
        {

      
            animator.SetTrigger("Exit"); // 触发动画

        float timeFloat = Array.Find(animator.runtimeAnimatorController.animationClips, clip => clip.name == "Exit").length;
        yield return new WaitForSeconds(timeFloat);


        isTransitioning = false; // 重置状态
        }


        // 触发事件回调
        OnAnimationCompleteEvent?.Invoke();
        //使用uimanager开启其他界面

        UIManager.Instance.OpenOtherUI(nextPageName);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
